package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAccountTrace;
import com.qf.service.DtsAccountTraceService;
import com.qf.util.ResponseUtil;
import com.qf.vo.AccountTraceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * projectName:workspace_6
 * author:hyn
 * time:2021/11/30 22:53
 * description:佣金管理
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/admin/brokerage")
public class AdminAccountTraceController {

    @Autowired
    private DtsAccountTraceService accountTraceService;
    //佣金列表
    @RequestMapping("list")
    public Object list(String username, String mobile,
                       @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(name = "limit",required = false,defaultValue = "20") Integer limit,
                       @RequestParam(name = "sort",required = false,defaultValue = "add_time") String sort,
                       @RequestParam(name = "order",required = false,defaultValue = "desc") String order,
                       @RequestParam(required = false,value = "statusArray[]") Integer[] statusArray
                        ){

        IPage<DtsAccountTrace> accountTraceIPage= accountTraceService.findAll(username,mobile,limit,page,sort,order,statusArray);
//        if(accountTraceIPage.getRecords().size() > 0){
        List list = new ArrayList<>();
        for(int i = 0;i<accountTraceIPage.getRecords().size();i++){
                AccountTraceVo accountTraceVo = new AccountTraceVo();
                accountTraceVo.setId(accountTraceIPage.getRecords().get(i).getId());
                accountTraceVo.setAmount(accountTraceIPage.getRecords().get(i).getAmount());
                accountTraceVo.setStatus(accountTraceIPage.getRecords().get(i).getStatus());
                accountTraceVo.setAddTime(accountTraceIPage.getRecords().get(i).getAddTime());
                accountTraceVo.setTotalAmount(accountTraceIPage.getRecords().get(i).getTotalAmount());
                accountTraceVo.setTraceSn(accountTraceIPage.getRecords().get(i).getTraceSn());
                accountTraceVo.setUserId(accountTraceIPage.getRecords().get(i).getUserId());
                list.add(accountTraceVo);
            }
//        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("total",accountTraceIPage.getTotal());
        data.put("traceList",list);
        return ResponseUtil.ok(data);

    }
    //佣金审批
    @RequestMapping("approve")
    public Object approve(@RequestBody DtsAccountTrace accountTrace){
        if(accountTrace == null){
            ResponseUtil.badArgument();
        }
        return accountTraceService.saveStatus(accountTrace);



    }
}
